package com.ruoyi.project.util;

import com.alibaba.fastjson.JSON;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.project.interface_log.domain.InterfaceLog;
import com.ruoyi.project.interface_log.service.IInterfaceLogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;


@Aspect
@Component
public class LoggingAspect {

    @Autowired
    private IInterfaceLogService interfaceLogService;

    @Autowired
    private HttpServletRequest request;
//
//    private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);
//
//    // 在方法执行前记录参数
//    @Before("@annotation(logParametersAndReturn)")
//    public void logMethodParameters(JoinPoint joinPoint, LogParametersAndReturn logParametersAndReturn) {
////        Object[] args = joinPoint.getArgs(); // 获取参数
////        String jsonArgs = JSON.toJSONString(args);
//
//
//    }
//
//    // 在方法执行后记录返回值
//    @AfterReturning(pointcut = "@annotation(logParametersAndReturn)", returning = "result")
//    public void logMethodReturnValue(LogParametersAndReturn logParametersAndReturn, Object result) {
////        String ip = null;
////        logger.info("返回值: {}", result);
////        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
////        if (attributes != null) {
////            HttpServletRequest request = attributes.getRequest();
////            ip = request.getRemoteAddr();
////        }
////        System.err.println("客户端IP：" + ip);
////        String value = logParametersAndReturn.value();
////        ;
////        System.err.println("写入的参数值：" + value);
////
////        String jsonResult = JSON.toJSONString(result);
////        System.err.println(jsonResult);
//    }


    // 在方法执行后记录返回值
    @Around("@annotation(logParametersAndReturn)")
    public Object logMethodAround(ProceedingJoinPoint joinPoint, LogParametersAndReturn logParametersAndReturn) throws Throwable {


        Object[] args = joinPoint.getArgs(); // 获取参数
        String jsonArgs = JSON.toJSONString(args);
        String requestIp = request.getRemoteAddr();
        Object proceed = joinPoint.proceed();
        String value = logParametersAndReturn.value();
        String jsonResult = JSON.toJSONString(proceed);


        InterfaceLog interfaceLog = new InterfaceLog();
        interfaceLog.setSystem(value);
        interfaceLog.setId(String.valueOf(UUID.randomUUID()));
        interfaceLog.setResult(jsonResult);
        interfaceLog.setSend(jsonArgs);
        String s = DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss");
        interfaceLog.setTime(s);
        interfaceLog.setIp(requestIp);
        interfaceLogService.insertInterfaceLog(interfaceLog);
        return proceed;
    }

}


